package com.javalsj.common.webflux.filter;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;

import com.javalsj.common.webflux.context.CommonWebFluxRequestContextHolder;

import reactor.core.publisher.Mono;

/**
 * @description 获取request内容过滤器，在 Filter 中直接将 request 存储到 WEBFLUX的Context 上下文中
 * @author WANGJIHONG
 * @date 2019年6月3日 上午9:15:14
 * @Copyright 版权所有 (c) www.javalsj.com
 * @memo 无备注说明
 */
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public class CommonWebFluxRequestContextFilter implements WebFilter, Ordered {

	@Override
	public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
		ServerHttpRequest request = exchange.getRequest();
		return chain.filter(exchange)
				.subscriberContext(ctx -> ctx.put(CommonWebFluxRequestContextHolder.CONTEXT_KEY, request));
	}

	@Override
	public int getOrder() {
		return HIGHEST_PRECEDENCE - 1;
	}
}
